package com.zqj.project.converter;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.dto.PhoneDTO;
import com.zqj.project.entity.PhonePO;
import com.zqj.project.model.Phone;
import com.zqj.project.vo.PhoneVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Description: PhoneConverter
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 20:53
 * @Version: 1.0
 */
public class PhoneConverter {

    private PhoneConverter() {
        throw new IllegalStateException("Utility Class");
    }

    public static PhoneVO toPhoneVo(Phone phone) {
        return BeanUtil.toBean(phone, PhoneVO.class);
    }

    public static Phone toPhoneModel(PhoneDTO phoneDto) {
        return BeanUtil.toBean(phoneDto, Phone.class);
    }

    public static Phone toPhoneModel(PhonePO phonePo) {
        return BeanUtil.toBean(phonePo, Phone.class);
    }

    public static PhonePO toPhonePo(Phone phone) {
        return BeanUtil.toBean(phone, PhonePO.class);
    }

    public static List<PhoneVO> toPhoneVoList(List<Phone> phoneModelList) {
        if (CollUtil.isEmpty(phoneModelList)) {
            return Collections.emptyList();
        }
        return phoneModelList.stream().map(e -> BeanUtil.toBean(e, PhoneVO.class)).toList();
    }

    public static List<Phone> toPhoneModelList(List<PhonePO> phonePoList) {
        if (CollUtil.isEmpty(phonePoList)) {
            return Collections.emptyList();
        }
        return phonePoList.stream().map(e -> BeanUtil.toBean(e, Phone.class)).toList();
    }

    public static IPage<PhoneVO> toPhoneVoPage(IPage<Phone> phoneModelPage) {
        IPage<PhoneVO> phoneVoPage = new Page<>(phoneModelPage.getCurrent(), phoneModelPage.getSize());
        List<PhoneVO> phoneVoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(phoneModelPage.getRecords())) {
            phoneModelPage.getRecords().forEach(entity -> phoneVoList.add(BeanUtil.toBean(entity, PhoneVO.class)));
            phoneVoPage.setRecords(phoneVoList);
            phoneVoPage.setTotal(phoneModelPage.getTotal());
        }
        return phoneVoPage;
    }

}